package com.example.springbootsfzy.controller.sunnan;

import com.example.springbootsfzy.domain.PublicWelfareActivities;
import com.example.springbootsfzy.service.sunnan.PublicWelfareActivitiesService;
import com.example.springbootsfzy.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/publicWelfareActivities")
public class PublicWelfareActivitiesController {

    @Autowired
    private PublicWelfareActivitiesService publicWelfareActivitiesService;

    /**
     * 分页查询公益活动
     * @param pageNum 当前页码
     * @param pageSize 每页显示的数量
     * @return PageInfo
     */
    @GetMapping
    @ResponseBody
    public PageInfo<PublicWelfareActivities> getPublicWelfareActivities(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return publicWelfareActivitiesService.getPublicWelfareActivities(pageNum, pageSize);
    }
    /**
     * 根据id查询公益活动
     * @param consultantId 公益活动id
     */
    @GetMapping("/{consultantId}")
    @ResponseBody
    public PageInfo<PublicWelfareActivities> getPublicWelfareActivitiesById(@RequestParam(defaultValue = "1") int pageNum,
                                                                            @RequestParam(defaultValue = "10")int pageSize,
                                                                            @PathVariable int consultantId) {
        return publicWelfareActivitiesService.getArticlesByConsultantId(pageNum, pageSize,consultantId);
    }

    /**
     * 添加公益活动
     * @param publicWelfareActivities 公益活动对象
     */
    @PostMapping
    @ResponseBody
    public R addPublicWelfareActivities(@RequestBody PublicWelfareActivities publicWelfareActivities) {
        if (publicWelfareActivitiesService.addPublicWelfareActivities(publicWelfareActivities) > 0) {
            return R.ok(null);
        } else {
            return R.fail("添加失败");
        }
    }
    /**
     * 根据id删除公益活动
     * @param id 公益活动id
     */
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable int id) {
        if (publicWelfareActivitiesService.deletePublicWelfareActivities(id) > 0) {
            return R.ok(null);
        } else {
            return R.fail("删除失败");
        }
    }
    /**
     * 修改公益活动
     * @param publicWelfareActivities 公益活动对象
     */

    @PutMapping
    @ResponseBody
    public R updatePublicWelfareActivities(@RequestBody PublicWelfareActivities publicWelfareActivities) {
        if (publicWelfareActivitiesService.updatePublicWelfareActivities(publicWelfareActivities) > 0) {
            return R.ok(null);
        } else {
            return R.fail("修改失败");
        }
    }


}
